import 'dart:io';

import 'package:device_info/device_info.dart';

///@date:  2021/02/24
///@author:  lixu
///@description:  设备信息管理
class DeviceUtils {
  DeviceUtils._();

  ///手机型号
  static String _model;

  ///手机品牌
  static String _brand;

  ///系统版本
  static String _osVersion;

  ///android or ios
  static String _osType;

  static Future<String> getBrand() async {
    await _initDeviceInfo();
    return _brand;
  }

  static Future<String> getModel() async {
    await _initDeviceInfo();
    return _model;
  }

  ///android 9
  static Future<String> getOsVersion() async {
    await _initDeviceInfo();
    return '${getOsType() + ' ' + _osVersion}';
  }

  static String getOsType() {
    if (_osType == null || _osType.length == 0) {
      _osType = Platform.operatingSystem;
    }
    return _osType;
  }

  static Future<void> _initDeviceInfo() async {
    if (_osType == null || _osType.length == 0) {
      _osType = Platform.operatingSystem ?? 'unKnow';
    }

    if (_osVersion == null || _model == null || _brand == null) {
      DeviceInfoPlugin plugin = DeviceInfoPlugin();
      if (Platform.isAndroid) {
        AndroidDeviceInfo build = await plugin.androidInfo;
        _osVersion = build.version.release;
        _model = build.model;
        _brand = build.brand;
      } else if (Platform.isIOS) {
        IosDeviceInfo build = await plugin.iosInfo;
        _osVersion = build.systemVersion;
        _model = build.model;
        _brand = build.name;
      } else {
        _osVersion = 'unKnow';
        _model = 'unKnow';
        _brand = 'unKnow';
      }
    }
  }

  static bool isIos() {
    return Platform.isIOS;
  }

  static bool isAndroid() {
    return Platform.isAndroid;
  }
}
